package com.example.demo;

import java.util.List;

import javax.persistence.LockModeType;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

// @CacheConfig(cacheNames = "users")
public interface UserRepository extends JpaRepository<User, Long> {

    //@Cacheable
    User findByName(String name);

    User findByNameAndAge(String name, Integer age);

    List<User> findByNameLike(String name);

    @Query("from User u where u.name=:name")
    User findUser(@Param("name") String name);

    Page<User> findAll(Specification<User> specification, Pageable page);

    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    @Query(value = "from User u where u.name=:name")
	public void getUserByNameForUpdate(@Param("name") String name);
}
